﻿using FB.Mvc.Controllers;
using FB.Providers;
using FCDB.Entity;
using FCDB.Entity.EntityEx;
using FCDB.Repository;
using FCDB.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wf5.Engine.Business;
using Wf5.Engine.Common;

namespace FCDB.Web.Controllers
{
     [FB.Membership.Annotations.AuthorizeOnly]
    public class ReadyTasksController : BaseController
    {
        //
        // GET: /ReadyTasks/
        private IWorkflowService service = new WorkflowService();
        private WfActivityInstanceTasksRepository respository = new WfActivityInstanceTasksRepository();
        private WfTasksRepository taskRepository = new WfTasksRepository();
        private WfProcessRepository processRepository = new WfProcessRepository();
        private WfProcessInstanceRepository wfpiResp = new WfProcessInstanceRepository();
        private WfActivityInstanceRepository wfactivityinstanceRespository = new WfActivityInstanceRepository();
        NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
        public ActionResult Index()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfActivityInstanceTasksEx> list = respository.GetTaskOfMine(this.UserID, skip, take, dtStart, dtEnd,
                    kewyword,UserIDs,DeparmentID,ProcessGuid,
                    out total); 
               
                foreach (var item in list)
                {
                     wfpiResp.GetProcessInstanceCreateUserName(item,this.UserID);
                    if (item.ReadyTaskType == 2)
                    {
                        item.AppName = "传阅：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.ReadyTaskType == 3)
                    {
                        item.AppName = "征求意见：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.IsReplay == 1)
                    {
                        item.AppName = "回复--传阅：" + item.AppName;
                       
                    }
                    if (item.IsReplay == 2)
                    {
                        item.AppName = "回复--征求意见：" + item.AppName;
                    }
                    if (item.ProcessGUID.ToString().Equals("0e37f0c1-737b-4653-93dd-d01a3fbc28f7"))
                    {
                        FormRepository repository = new FormRepository();
                        FormCDB fcdb = repository.GetForm<FormCDB>(item.AppInstanceID);
                        if(fcdb.financiaAmount == null || fcdb.financiaAmount == 0){
                            item.isRed = "【未收款】";
                        }else
                        {
                            item.isRed = "";
                        }
                    }
                    else
                    {
                        item.isRed = "";

                    }

                }
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;
            } 
               WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
               IList<WfProcess> processList = WFRespository.GetAllProcess();
               ViewBag.ProcessList = processList;
            return View();
        }

        public ActionResult ViewReadyTask()
        {
            if (string.IsNullOrEmpty(Request["AppInstanceID"])) return Content("没有存在该表单");
            Guid ProcessGUID = new Guid(Request["ProcessGUID"]);
            string ActivityGUID = Request["ActivityGUID"];
            string AppInstanceID = Request["AppInstanceID"];
            string ReadyTaskType = Request["ReadyTaskType"];
            WfProcess process = processRepository.GetById(ProcessGUID);
            try
            {
               
                WfAppRunner wfAppRunner = new WfAppRunner()
                    {
                        AppInstanceID = long.Parse(AppInstanceID),
                        ProcessGUID = ProcessGUID,
                        UserID = this.UserID,
                        UserName = this.UserName
                    };
                service.ReadTask(wfAppRunner);
            }
            catch (Exception ex)
            {
                _logger.ErrorException(ex.Message, ex);
            }
            string url = "~/{0}?ProcessGuid={1}&AppInstanceID={2}&&ActivityGuid={3}";
             
            if (long.Parse(ReadyTaskType) == 2 || long.Parse(ReadyTaskType) == 3 )
            {
                url += "&&ViewDetail=" + (int)EnumFormView.DocumentLog;
            }
            url = string.Format(url, process.PageUrl, ProcessGUID, AppInstanceID, ActivityGUID);
            return Redirect(url);
             
        }

        public ActionResult editFinance()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfActivityInstanceTasksEx> list = respository.GetTaskOfFinance( skip, take, dtStart, dtEnd,
                    kewyword, UserIDs, DeparmentID, ProcessGuid,
                    out total);

                foreach (var item in list)
                {
                    wfpiResp.GetProcessInstanceCreateUserName(item, this.UserID);
                    if (item.ReadyTaskType == 2)
                    {
                        item.AppName = "传阅：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.ReadyTaskType == 3)
                    {
                        item.AppName = "征求意见：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.IsReplay == 1)
                    {
                        item.AppName = "回复--传阅：" + item.AppName;

                    }
                    if (item.IsReplay == 2)
                    {
                        item.AppName = "回复--征求意见：" + item.AppName;
                    }
                    if (item.ProcessGUID.ToString().Equals("0e37f0c1-737b-4653-93dd-d01a3fbc28f7"))
                    {
                        FormRepository repository = new FormRepository();
                        FormCDB fcdb = repository.GetForm<FormCDB>(item.AppInstanceID);
                        if (fcdb.financiaAmount == null || fcdb.financiaAmount == 0)
                        {
                            item.isRed = "【未收款】";
                        }
                        else
                        {
                            item.isRed = "";
                        }
                    }
                }
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;
            }
            WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
            IList<WfProcess> processList = WFRespository.GetAllProcess();
            ViewBag.ProcessList = processList;
            return View();
        }
        public ActionResult financePrint()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfActivityInstanceTasksEx> list = respository.GetTaskOfFinance( skip, take, dtStart, dtEnd,
                    kewyword, UserIDs, DeparmentID, ProcessGuid,
                    out total);

                foreach (var item in list)
                {
                    wfpiResp.GetProcessInstanceCreateUserName(item, this.UserID);
                    if (item.ReadyTaskType == 2)
                    {
                        item.AppName = "传阅：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.ReadyTaskType == 3)
                    {
                        item.AppName = "征求意见：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.IsReplay == 1)
                    {
                        item.AppName = "回复--传阅：" + item.AppName;

                    }
                    if (item.IsReplay == 2)
                    {
                        item.AppName = "回复--征求意见：" + item.AppName;
                    }
                    if (item.ProcessGUID.ToString().Equals("0e37f0c1-737b-4653-93dd-d01a3fbc28f7"))
                    {
                        FormRepository repository = new FormRepository();
                        FormCDB fcdb = repository.GetForm<FormCDB>(item.AppInstanceID);
                        if (fcdb.financiaAmount == null || fcdb.financiaAmount == 0)
                        {
                            item.isRed = "【未收款】";
                        }
                        else
                        {
                            item.isRed = "";
                        }
                    }
                }
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;
            }
            WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
            IList<WfProcess> processList = WFRespository.GetAllProcess();
            ViewBag.ProcessList = processList;
            return View();
        }
         

        public ActionResult editComprehensive()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfActivityInstanceTasksEx> list = respository.GetTaskOfComprehensive(skip, take, dtStart, dtEnd,
                    kewyword, UserIDs, DeparmentID, ProcessGuid,
                    out total);

                foreach (var item in list)
                {
                    wfpiResp.GetProcessInstanceCreateUserName(item, this.UserID);
                    if (item.ReadyTaskType == 2)
                    {
                        item.AppName = "传阅：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.ReadyTaskType == 3)
                    {
                        item.AppName = "征求意见：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.IsReplay == 1)
                    {
                        item.AppName = "回复--传阅：" + item.AppName;

                    }
                    if (item.IsReplay == 2)
                    {
                        item.AppName = "回复--征求意见：" + item.AppName;
                    }
                    if (item.ProcessGUID.ToString().Equals("0e37f0c1-737b-4653-93dd-d01a3fbc28f7"))
                    {
                        FormRepository repository = new FormRepository();
                        FormCDB fcdb = repository.GetForm<FormCDB>(item.AppInstanceID);
                        if (fcdb.financiaAmount == null || fcdb.financiaAmount == 0)
                        {
                            item.isRed = "【未收款】";
                        }
                        else
                        {
                            item.isRed = "";
                        }
                    }
                }
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;
            }
            WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
            IList<WfProcess> processList = WFRespository.GetAllProcess();
            ViewBag.ProcessList = processList;
            return View();
        }

        public ActionResult editBusiness()
        {
            if (Request.IsAjaxRequest())
            {
                int total = 0, take = 10, skip = 0;
                int.TryParse(Request["take"], out take);
                int.TryParse(Request["skip"], out skip);
                string dtStart = Request["dtStart"];
                string dtEnd = Request["dtEnd"];
                string kewyword = Request["keyword"];
                string UserIDs = Request["UserIDs"];
                string DeparmentID = Request["DeparmentID"];
                string ProcessGuid = Request["ProcessGUID"];
                IList<WfActivityInstanceTasksEx> list = respository.GetTaskOfMineBusiness(this.UserID, skip, take, dtStart, dtEnd,
                    kewyword, UserIDs, DeparmentID, ProcessGuid,
                    out total); 

                foreach (var item in list)
                {
                    wfpiResp.GetProcessInstanceCreateUserName(item, this.UserID);
                    if (item.ReadyTaskType == 2)
                    {
                        item.AppName = "传阅：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.ReadyTaskType == 3)
                    {
                        item.AppName = "征求意见：" + item.AppName;
                        item.ActivityName = GetEmployeeByUserKey(item.SendUserID.Value).ChineseName;
                        item.AppInstanceCreateUserName = item.CreatedByUserName;
                    }
                    if (item.IsReplay == 1)
                    {
                        item.AppName = "回复--传阅：" + item.AppName;

                    }
                    if (item.IsReplay == 2)
                    {
                        item.AppName = "回复--征求意见：" + item.AppName;
                    }
                    if (item.ProcessGUID.ToString().Equals("0e37f0c1-737b-4653-93dd-d01a3fbc28f7"))
                    {
                        FormRepository repository = new FormRepository();
                        FormCDB fcdb = repository.GetForm<FormCDB>(item.AppInstanceID);
                        if (fcdb.financiaAmount == null || fcdb.financiaAmount == 0)
                        {
                            item.isRed = "【未收款】";
                        }
                        else
                        {
                            item.isRed = "";
                        }
                    }
                }
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = total
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                };
                return json;
            }
            WFWorkFlowRepository WFRespository = new WFWorkFlowRepository();
            IList<WfProcess> processList = WFRespository.GetAllProcess();
            ViewBag.ProcessList = processList;
            return View();
        }
    }
}
